/*
上传离线订单和支付 2.0
 */
import 'package:reabampos/src/reabam/operation/bean/h5_upload_order.dart';
import 'package:reabampos/src/reabam/operation/db/objectbox.dart';
import 'package:reabampos/src/reabam/ui/pagestate_main_webview.dart';
import 'package:reabampos/src/reabam/utils/rb_aliyun_log.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_aliyun_log.dart';
import 'package:xsdk_flutter/xsdk/xsdk_flutter_package.dart';

abstract class SupportUploadOrderAndPay {
  //上传订单任务 2.0
  static Map<String, dynamic> uploadOrderTask2(dynamic data) {
    Map<String, dynamic> mapItem;
    if (data is String) {
      mapItem = XJsonUtils.json2Map(data);
    } else {
      mapItem = data;
    }

    String? fastOrderNo = mapItem['fastOrderNo'];
    XLogUtils.printLog(
        '---收到h5通知上传订单2.0任务: fastOrderNo=$fastOrderNo, data=$data');
    //日志
    XAliyunLogUtils.sendLog(
        RBAliyunLogUtils.getAliyunLogKey_UploadOrder(fastOrderNo),
        '收到上传订单2.0任务: fastOrderNo=$fastOrderNo, data=$data');

    //添加任务队列
    Map<String, dynamic> result =
        XObjectBoxUtils.saveUploadOrderTaskByTransaction(mapItem);

    XLogUtils.printLog(
        '---通知上传订单2.0任务保存结果:result=${XJsonUtils.obj2Json(result)}');

    return result;
  }

  //h5检查订单是否已追平
  static void checkOrderUpload(
      dynamic data, XFunctionObjectBoxCallback callback) {
    String orderNo = '$data';
    XLogUtils.printLog('---h5检查订单是否已追平,orderNo=$orderNo');
    //日志
    XAliyunLogUtils.sendLog(
        RBAliyunLogUtils.getAliyunLogKey_UploadOrder(orderNo),
        '收到h5检查订单是否已追平checkOrderUpload方法调用!');

    XObjectBoxUtils.checkUploadOrder(orderNo, callback);
  }

  //获得未上传订单数
  static void checkUnUploadOrderCnt(XPageState statePage) {
    Future(() {
      int count = XObjectBoxUtils.checkUnUploadOrderCnt();
      XLogUtils.printLog('---checkUnUploadOrderCnt=$count');
      if (statePage is MainWebViewStatePage) {
        statePage.runJSFunction(statePage.jsFunction_getAppValue(
            'checkUnUploadOrderCnt', '$count'));
      }
    });
  }

  //获得本地未追平订单列表ok
  static void getUploadOrderList(
      dynamic data, XFunctionObjectBoxCallback callback) {
    BeanH5RequestPageLocalOrder beanH5RequestPageLocalOrder;
    if (data is String) {
      beanH5RequestPageLocalOrder =
          BeanH5RequestPageLocalOrder.fromJson(XJsonUtils.json2Map(data));
    } else {
      beanH5RequestPageLocalOrder = BeanH5RequestPageLocalOrder.fromJson(data);
    }

    XLogUtils.printLog('---查找未追平订单列表: data=$data');
    //
    XObjectBoxUtils.getUploadOrderList(beanH5RequestPageLocalOrder, callback);
  }

  //刷新本地未追平订单, POS传输业务重试(批量/单个都支持) ok
  static void retryPosOrder(dynamic data, XFunctionObjectBoxCallback callback) {
    //
    String orderNo = '$data';

    //日志
    XAliyunLogUtils.sendLog(
        RBAliyunLogUtils.getAliyunLogKey_UploadOrder(orderNo),
        '收到h5刷新本地未追平订单, orderNo=$orderNo');

    if (!XStringUtils.isEmpty(orderNo)) {
      XObjectBoxUtils.retryOrder(orderNo, callback);
    }
  }

  //获得购物车 ok
  static void getOrderShoppingcart(
      dynamic data, XFunctionObjectBoxCallback callback) {
    String orderNo = '$data';
    //
    if (!XStringUtils.isEmpty(orderNo)) {
      XObjectBoxUtils.getOrderShoppingcart(orderNo, callback);
    }
  }

  //保存购物车ok
  static void saveOrderShoppingcart(
      dynamic data, XFunctionObjectBoxCallback callback) {
    BeanH5UploadOrderTask beanH5UploadOrderTask;
    if (data is String) {
      beanH5UploadOrderTask =
          BeanH5UploadOrderTask.fromJson(XJsonUtils.json2Map(data));
    } else {
      beanH5UploadOrderTask = BeanH5UploadOrderTask.fromJson(data);
    }

    XLogUtils.printLog(
        '---收到h5通知保存购物车: fastOrderNo=${beanH5UploadOrderTask.fastOrderNo}, data=$data');
    //日志
    XAliyunLogUtils.sendLog(
        RBAliyunLogUtils.getAliyunLogKey_UploadOrder(
            beanH5UploadOrderTask.fastOrderNo),
        '收到h5通知保存购物车: fastOrderNo=${beanH5UploadOrderTask.fastOrderNo}, data=$data');
    //
    XObjectBoxUtils.saveOrderShoppingcart(beanH5UploadOrderTask, callback);
  }

  //登录成功重新检测和处理未上传订单2.0
  static void loginInitCheckUnUploadOrderTaskX() async {
    Future(() {
      //清理上传订单(已追平)
      XObjectBoxUtils.checkAndDeleteUploadOrder();
      //清除今天以前的旧购物车
      XObjectBoxUtils.removeOldOrderShoppingcart();
      //登录成功重新检测和处理未上传订单2.0
      XObjectBoxUtils.loginInitCheckUnUploadOrderTaskX();

      //清除过期异常订单
      XObjectBoxUtils.delYCOrder();
      //检测异常订单支付
      XObjectBoxUtils.checkYCOrderPay();
    });
  }
}
